package toDoList.Controllers;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import toDoList.Manager.DataBaseHelper;
import toDoList.Manager.Logger;
import toDoList.Manager.R;
import toDoList.Manager.R.id;
import toDoList.Manager.R.layout;
import toDoList.Models.ToDoItemModel;
import toDoList.Models.UserModel;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.view.KeyEvent;

public class ToDoListController extends Activity implements ControllerInterface {

	private String searchString;
	private ArrayList<ToDoItemModel> toDoItems;
	private UserModel user;
	DataBaseHelper helper;
	
	/**
	 * Runs when the activity is first created
	 */
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.todolist);
        
        user = new UserModel(this.getIntent().getStringExtra("name"), this.getIntent().getStringExtra("password"), this.getIntent().getStringExtra("email"), this.getIntent().getStringExtra("userGUID"));
        toDoItems = new ArrayList<ToDoItemModel>();
        helper = new DataBaseHelper(this);
        
        //Update the tasks spinner
        Spinner recSpinner = (Spinner)findViewById(R.id.taskSpinner);
		ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.tasks_array, android.R.layout.simple_spinner_item);
		recSpinner.setAdapter(adapter2);
		
		updateList();
    }
	
	@Override
	public void onWindowFocusChanged(boolean it)
	{
		super.onWindowFocusChanged(true);
		updateCheckBoxes();
	}
	
	/**
	 * clearText(View v) Clears the text from the search box
	 * @param v The current view
	 */
	public void clearText(View v)
	{
		if(((TextView)findViewById(R.id.toDoListSearchText)).getText().toString().equals("Search..."))
			((TextView)findViewById(R.id.toDoListSearchText)).setText("");
	}
	
	public void showMap(View v)
	{
		Intent i = new Intent(ToDoListController.this, MapController.class);
		i.putExtra("user", user.getGUID());
		startActivityForResult(i, 2);
	}
	
	/**
	 * search(View v) Runs a search and updates the list with the results
	 * @param v The current view
	 */
	public void search(View v)
	{
		if(((EditText)findViewById(R.id.toDoListSearchText)).getText().toString().toLowerCase().equals("today"))
		{
			updateListDate();
			popUp("This will show all items dated after today.");
		}
		else if(!((EditText)findViewById(R.id.toDoListSearchText)).getText().toString().equals(""))
		{
			try
			{
				updateList(((EditText)findViewById(R.id.toDoListSearchText)).getText().toString());
				popUp("This will show all items with a category or name similar to the search text: " + ((EditText)findViewById(R.id.toDoListSearchText)).getText().toString());
				
			}
			catch(Exception e)
			{
				popUp("Couldn't update the list.");
			}
		}
		else
		{
			updateList();
			popUp("The list has been updated.");
		}
	}
	
	/**
	 * updateList(String string) Updates the to-do list based on the strings input into the search box
	 * @param string The string to search for when updating the list
	 */
	public void updateList(String string) {
		
		toDoItems.clear();
		
		//Tokenize the string
		String[] words = string.split(" ");
		
		//Get the readable database
		SQLiteDatabase db = helper.getReadableDatabase();	
		
		//Create a cursor to use to look through the database
		Cursor mCursor;
		
		String whereClause = "";
		for(int i = 0; i < words.length; i++)
		{
			if(!whereClause.equals(""))
			{
				whereClause += " or ";
			}
			
			whereClause += "ToDoItem.name like '" + words[i] + "' or Category.name like '" + words[i] + "'";
		}
		
		//Then check the database based on each of these words
		//This means loop through each word.. check the categories and the dates of each item.

		Logger.writeLog("SELECT * FROM Category LEFT OUTER JOIN ToDoItem on ToDoItem.catGUID = Category.catGUID WHERE userGUID= '" + user.getGUID() + "' AND (" + whereClause + ")");
		mCursor = db.rawQuery("SELECT * FROM Category LEFT OUTER JOIN ToDoItem on ToDoItem.catGUID = Category.catGUID WHERE userGUID= '" + user.getGUID() + "' AND (" + whereClause + ")", new String[] {});	
	    if (mCursor != null) {
		      mCursor.moveToFirst();
	    }
	    
	    
		ListView v = ((ListView)findViewById(R.id.toDoItemList));
		for(int i = 0; i < mCursor.getCount(); i++)
		{
			if(mCursor.getPosition() < mCursor.getCount() - 1)
			{
				if(((Spinner)findViewById(R.id.taskSpinner)).getSelectedItem().toString().equals("All"))
				{
					toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					mCursor.moveToNext();
				}
				else if(((Spinner)findViewById(R.id.taskSpinner)).getSelectedItem().toString().equals("Complete"))
				{
					if(mCursor.getString(mCursor.getColumnIndex("checked")).toString().equals("true"))
					{
						toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					}
					mCursor.moveToNext();
				}
				else
				{
					if(mCursor.getString(mCursor.getColumnIndex("checked")).toString().equals("false"))
					{
						toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					}
					mCursor.moveToNext();
				}
			}
			else
			{
				if(((Spinner)findViewById(R.id.taskSpinner)).getSelectedItem().toString().equals("All"))
				{
					toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
				}
				else if(((Spinner)findViewById(R.id.taskSpinner)).getSelectedItem().toString().equals("Complete"))
				{
					if(mCursor.getString(mCursor.getColumnIndex("checked")).toString().equals("true"))
					{
						toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					}
				}
				else
				{
					if(mCursor.getString(mCursor.getColumnIndex("checked")).toString().equals("false"))
					{
						toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					}
				}
			}
		}
			
		ArrayAdapter<ToDoItemModel> array = new ArrayAdapter<ToDoItemModel>(this, R.layout.todoitem, R.id.toDoItemTextbox);
		for(int i = 0; i < toDoItems.size(); i++)
			array.add(toDoItems.get(i));
		v.setAdapter(array);
		array.notifyDataSetChanged();
		
		//Close the database
	    db.close();
	}
	
	/**
	 * updateListDate() Updates the list with all of the items dated after the current date
	 */
	public void updateListDate()
	{
		toDoItems.clear();
		
		SQLiteDatabase db = helper.getReadableDatabase();	
		//Get all the to-do items
		DateFormat dateFormat = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss.sss");
		Date date = new Date();
		Cursor mCursor = db.query("ToDoItem", new String[] {"name", "description", "catGUID", "userGUID", "toDoItemGUID", "checked", "startDateTime", "location"}, "userGUID = '" + user.getGUID() + "' AND startDateTime BETWEEN '" +  dateFormat.format(date) + "' AND '2130-01-01 11:11:11.111'", null, null, null, null);
	    if (mCursor != null) {
		      mCursor.moveToFirst();
	    }
	    db.close();
	    
	    ListView v = ((ListView)findViewById(R.id.toDoItemList));
		for(int i = 0; i < mCursor.getCount(); i++)
		{
			if(mCursor.getPosition() < mCursor.getCount() - 1)
			{
				if(((Spinner)findViewById(R.id.taskSpinner)).getSelectedItem().toString().equals("All"))
				{
					toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					mCursor.moveToNext();
				}
				else if(((Spinner)findViewById(R.id.taskSpinner)).getSelectedItem().toString().equals("Complete"))
				{
					if(mCursor.getString(mCursor.getColumnIndex("checked")).toString().equals("true"))
					{
						toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					}
					mCursor.moveToNext();
				}
				else
				{
					if(mCursor.getString(mCursor.getColumnIndex("checked")).toString().equals("false"))
					{
						toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					}
					mCursor.moveToNext();
				}
			}
			else
			{
				if(((Spinner)findViewById(R.id.taskSpinner)).getSelectedItem().toString().equals("All"))
				{
					toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
				}
				else if(((Spinner)findViewById(R.id.taskSpinner)).getSelectedItem().toString().equals("Complete"))
				{
					if(mCursor.getString(mCursor.getColumnIndex("checked")).toString().equals("true"))
					{
						toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					}
				}
				else
				{
					if(mCursor.getString(mCursor.getColumnIndex("checked")).toString().equals("false"))
					{
						toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					}
				}
			}
	    }
		ArrayAdapter<ToDoItemModel> array = new ArrayAdapter<ToDoItemModel>(this, R.layout.todoitem, R.id.toDoItemTextbox);	
		for(int i = 0; i < toDoItems.size(); i++)
			array.add(toDoItems.get(i));
		v.setAdapter(array);
	}

	/**
	 * createListener() Ties all of the controllers together
	 */
	public void createListener()
	{
		throw new UnsupportedOperationException("The method is not implemented yet.");
	}
	
	/**
	 * addItem(View v) Starts a ToDoItemController activity. Allows the user to add a new item to the database.
	 * @param v The current view
	 */
	public void addItem(View v)
	{
		Intent myIntent = new Intent(ToDoListController.this, ToDoItemController.class);
		startActivityForResult(myIntent, 1);
	}
	
	/**
	 * onActivityResult(int requestCode, int resultCode, Intent data) Based on the requestCode and resultCode, it will do an activiy after a sub-activity ends
	 * @param requestCode The requestCode passed back by the sub-activity
	 * @param resultCode The resultCode passed back by the sub-activity
	 * @param data The data passed back by the sub-activity
	 */
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		switch(requestCode)
		{
			case (1):
			{
				if(resultCode == RESULT_OK)
				{
					//Find the category GUID for the category name
					String name = data.getStringExtra("category");
					Logger.writeLog(name);
					String catGUID = catGUIDByName(name);
					
					try 
					{
				 		SQLiteDatabase db = helper.getWritableDatabase();	
				 		db.execSQL("INSERT INTO ToDoItem (name, description, catGUID, userGUID, toDoItemGUID, startDateTime, checked, location) " + "VALUES (" + "'"+ data.getStringExtra("title") + "'" + ", " + "'"+ data.getStringExtra("description") + "', '" + catGUID + "', '" + user.getGUID() + "', '" + UUID.randomUUID() + "', '" + data.getStringExtra("dateTime") + "', 'false'," + "'" + data.getStringExtra("location") + "')");
				 		db.close();
					}
					catch(Exception e)
					{
						popUp(e.toString());
					}
					
					updateList();
				}
			}
		}
	}
	
	/**
	 * catGUIDByName(String name) Gets the category's guid by using it's name to search
	 * @param name The name of the category
	 * @return The GUID of the category
	 */
	public String catGUIDByName(String name) {
		
		SQLiteDatabase db = helper.getReadableDatabase();
		
		Cursor mCursor = db.query(true, "Category", new String[] {"name", "catGUID"}, "name = '" + name + "'", null, null, null, null, null);
		    if (mCursor != null) {
		      mCursor.moveToFirst();
		    }
		    db.close();
		    
		    return mCursor.getString(mCursor.getColumnIndex("catGUID"));
	}

	/**
	 * itemChecked(View v) Runs when a checkbox is clicked inside one of the list items
	 * @param v The current view
	 */
	public void itemChecked(View v)
	{
		for(int i = 0; i < toDoItems.size(); i++)
		{
			if(((TextView)(((LinearLayout)v.getParent()).getChildAt(1))).getText().toString().equals(toDoItems.get(i).toString()))
			{
				SQLiteDatabase db = helper.getWritableDatabase();
				if(!(((CheckBox)((LinearLayout)v.getParent()).getChildAt(0)).isChecked()))
				{
					db.execSQL("UPDATE ToDoItem SET checked = 'false' WHERE toDoItemGUID = '" + toDoItems.get(i).getGUID().toString() + "'");
					Logger.writeLog("ToDoList item " + toDoItems.get(i).toString() + "is updated to be NOT CHECKED");
					((CheckBox)((LinearLayout)v.getParent()).getChildAt(0)).setChecked(false);
				}
				if(((CheckBox)((LinearLayout)v.getParent()).getChildAt(0)).isChecked())
				{
					db.execSQL("UPDATE ToDoItem SET checked = 'true' WHERE toDoItemGUID = '" + toDoItems.get(i).getGUID().toString() + "'");
					Logger.writeLog("ToDoList item " + toDoItems.get(i).toString() + "is updated to be CHECKED");
					((CheckBox)((LinearLayout)v.getParent()).getChildAt(0)).setChecked(true);
				}
			}
		}
	}
	
	/**
	 * updateCheckBoxes() Updates the checkbox on each listitem
	 */
	public void updateCheckBoxes()
	{
		SQLiteDatabase db = helper.getReadableDatabase();
		
		Cursor cursor = db.query("ToDoItem", new String[] {"toDoItemGUID", "checked"}, "checked = 'true'", null, null, null, null);
		if (cursor != null) {
		      cursor.moveToFirst();
	    }
	    db.close();
	    
	    if(cursor.getCount() != 0)
	    {
		    for(int i = 0; i < cursor.getCount(); i++)
		    {
		    	for(int j = 0; j < toDoItems.size(); j++)
		    	{
			    	if(toDoItems.get(j).getGUID().toString().equals(cursor.getString(cursor.getColumnIndex("toDoItemGUID"))))
			    	{
			    		    ListView lv = ((ListView)findViewById(R.id.toDoItemList));
			    		    LinearLayout ll = ((LinearLayout)lv.getChildAt(j));
			    		    
			    		    if(ll != null)
			    		    {
			    		    	CheckBox cb = ((CheckBox)(ll.getChildAt(0)));
			    		    	cb.setChecked(true);
			    		    }
			    		    
			    			if(cursor.getPosition() < cursor.getCount() - 1)
					    	{
					    		cursor.moveToNext();
					    	}
			    	}
		    	}
		    }
	    }
	}
	
	/**
	 * updateList() Updates the list with all of the items available
	 */
	private void updateList() {
		
		toDoItems.clear();
		
		SQLiteDatabase db = helper.getReadableDatabase();	
		//Get all the to-do items
		Cursor mCursor = db.query("ToDoItem", new String[] {"name", "description", "catGUID", "userGUID", "toDoItemGUID", "checked", "location"}, "userGUID = '" + user.getGUID() + "'", null, null, null, null);
	    if (mCursor != null) {
		      mCursor.moveToFirst();
	    }
	    db.close();
	    
	    
	    ListView v = ((ListView)findViewById(R.id.toDoItemList));
		for(int i = 0; i < mCursor.getCount(); i++)
		{
			if(mCursor.getPosition() < mCursor.getCount() - 1)
			{
				if(((Spinner)findViewById(R.id.taskSpinner)).getSelectedItem().toString().equals("All"))
				{
					toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					mCursor.moveToNext();
				}
				else if(((Spinner)findViewById(R.id.taskSpinner)).getSelectedItem().toString().equals("Complete"))
				{
					if(mCursor.getString(mCursor.getColumnIndex("checked")).toString().equals("true"))
					{
						toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					}
					mCursor.moveToNext();
				}
				else
				{
					if(mCursor.getString(mCursor.getColumnIndex("checked")).toString().equals("false"))
					{
						toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					}
					mCursor.moveToNext();
				}
			}
			else
			{
				if(((Spinner)findViewById(R.id.taskSpinner)).getSelectedItem().toString().equals("All"))
				{
					toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
				}
				else if(((Spinner)findViewById(R.id.taskSpinner)).getSelectedItem().toString().equals("Complete"))
				{
					if(mCursor.getString(mCursor.getColumnIndex("checked")).toString().equals("true"))
					{
						toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					}
				}
				else
				{
					if(mCursor.getString(mCursor.getColumnIndex("checked")).toString().equals("false"))
					{
						toDoItems.add(new ToDoItemModel(mCursor.getString(mCursor.getColumnIndex("name")), mCursor.getString(mCursor.getColumnIndex("description")), 0, mCursor.getString(mCursor.getColumnIndex("catGUID")), mCursor.getString(mCursor.getColumnIndex("userGUID")), mCursor.getString(mCursor.getColumnIndex("toDoItemGUID")), mCursor.getString(mCursor.getColumnIndex("checked")), mCursor.getString(mCursor.getColumnIndex("location"))));
					}
				}
			}
	    }
		ArrayAdapter<ToDoItemModel> array = new ArrayAdapter<ToDoItemModel>(this, R.layout.todoitem, R.id.toDoItemTextbox);
		for(int i = 0; i < toDoItems.size(); i++)
			array.add(toDoItems.get(i));
		v.setAdapter(array);
		array.notifyDataSetChanged();
	}

	/**
	 * popUp(String n) Displays a pop-up window with the message "n" inside of it.
	 * @param n The string to display
	 */
	public void popUp(String n) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(n)
		       .setCancelable(false)
		       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });
		
		builder.show();
	}

}
